package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.em.model.RunningRecord;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.RunningRecordDao;
import cn.b.sky.mes.em.model.condition.RunningRecordCondition;
import cn.b.sky.mes.em.service.RunningRecordService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("runningRecordService")
public class RunningRecordServiceImpl implements RunningRecordService {

  private static final Logger LOG = LoggerFactory.getLogger(RunningRecordServiceImpl.class);

  @Resource
  RunningRecordDao recordDao;

  @Override
  public List<RunningRecord> selectRunningRecordListByCondition(RunningRecordCondition condition) {
    if (condition == null) {
      condition = new RunningRecordCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
      condition.setOrderDirection("desc");
    }
    return recordDao.selectRunningRecordListByCondition(condition);
  }

  @Override
  public int selectRunningRecordCountByCondition(RunningRecordCondition condition) {
    if (condition == null) {
      condition = new RunningRecordCondition();
    }
    return recordDao.selectRunningRecordCountByCondition(condition);
  }

  @Override
  public RunningRecord getRunningRecord(Long id) {
    if (id >= 0) {
      return recordDao.getRunningRecord(id);
    }
    return null;
  }

  @Override
  public boolean insertRunningRecord(RunningRecord record) {
    if (StringUtils.isBlank(record.getEquipmentCode()) || StringUtils.isBlank(record.getStatusCode())) {
      return false;
    } else {
      recordDao.insertRunningRecord(record);
      return true;
    }
  }

  @Override
  public boolean updateRunningRecord(RunningRecord record) {
    if (record.getId() == null) {
      return false;
    } else {
      recordDao.updateRunningRecord(record);
      return true;
    }
  }

  @Override
  public BatchJobReport deleteRunningRecord(Long id, String siteCode) {
    if (id != null) {
      String message = "";
      String cause = "";
      boolean success = true;
      try {
        recordDao.deleteRunningRecord(id, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(e.getMessage())) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    } else {
      return new BatchJobReport("", "equipment is null", false);
    }
  }

}
